package org.jeecg.modules.relation.relclassstudent.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.jeecg.common.api.vo.Result;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.modules.classes.entity.CouClass;
import org.jeecg.modules.classes.service.ICouClassService;
import org.jeecg.modules.common.api.ISysInfo;
import org.jeecg.modules.common.utils.StringUtil;
import org.jeecg.modules.relation.relclassstudent.entity.RelClassStudent;
import org.jeecg.modules.relation.relclassstudent.service.IRelClassStudentService;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;

import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.modules.relation.relclassstudent.vo.RelClassesAndStudentsVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.jeecg.common.aspect.annotation.AutoLog;

/**
 * @Description: rel_class_student
 * @author: baochangrong
 * @Date: 2022-02-10
 * @Version: V1.0
 */
@Api(tags = "rel_class_student")
@RestController
@RequestMapping("/rel_class_student/relClassStudent")
@Slf4j
public class RelClassStudentController extends JeecgController<RelClassStudent, IRelClassStudentService> {
    @Autowired
    private IRelClassStudentService relClassStudentService;
    @Autowired
    private ISysInfo sysInfo;

    private ICouClassService couClassService;

    /**
     * 分页列表查询
     *
     * @param relClassStudent
     * @param pageNo
     * @param pageSize
     * @param req
     * @return
     */
//    @AutoLog(value = "rel_class_student-分页列表查询")
//    @ApiOperation(value = "rel_class_student-分页列表查询", notes = "rel_class_student-分页列表查询")
//    @GetMapping(value = "/list")
//    public Result<?> queryPageList(RelClassStudent relClassStudent,
//                                   @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
//                                   @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
//                                   HttpServletRequest req) {
//
//        /**
//         * _t --- [Ljava.lang.String;@e59a48b
//         * column --- [Ljava.lang.String;@178f1048
//         * order --- [Ljava.lang.String;@29a3b46a
//         * field --- [Ljava.lang.String;@4f16f18f
//         * pageNo --- [Ljava.lang.String;@ca8791a
//         * pageSize --- [Ljava.lang.String;@634a3597
//         */
//        QueryWrapper<RelClassStudent> queryWrapper = QueryGenerator.initQueryWrapper(relClassStudent, req.getParameterMap());
//        Page<RelClassStudent> page = new Page<RelClassStudent>(pageNo, pageSize);
//        IPage<RelClassStudent> pageList = relClassStudentService.page(page, queryWrapper);
//        return Result.OK(pageList);
//    }
    @AutoLog(value = "rel_class_student-分页列表查询")
    @ApiOperation(value = "rel_class_student-分页列表查询", notes = "rel_class_student-分页列表查询")
    @GetMapping(value = "/list")
    public Result<?> queryPageVoList(RelClassesAndStudentsVO relClassStudent,
                                     @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                     @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                     HttpServletRequest req) {
        String tenantId = req.getHeader("tenant-id");
        Page<RelClassesAndStudentsVO> page = new Page<>(pageNo, pageSize);
        IPage<RelClassesAndStudentsVO> pageList = relClassStudentService.queryVo(page, relClassStudent, tenantId);
        return Result.OK(pageList);
//        QueryWrapper<RelClassStudent> queryWrapper = QueryGenerator.initQueryWrapper(relClassStudent, req.getParameterMap());
//        Page<RelClassStudent> page = new Page<RelClassStudent>(pageNo, pageSize);
//        IPage<RelClassStudent> pageList = relClassStudentService.page(page, queryWrapper);
//        return Result.OK(pageList);
    }


    /**
     * 添加
     *
     * @param relClassStudent
     * @return
     */
    @AutoLog(value = "rel_class_student-添加")
    @ApiOperation(value = "rel_class_student-添加", notes = "rel_class_student-添加")
    @PostMapping(value = "/add")
    public Result<?> add(@RequestBody RelClassStudent relClassStudent) {
        CouClass couClass = couClassService.getById(relClassStudent.getClassId());
        relClassStudent.setClassName(couClass.getClassName());
        relClassStudentService.save(relClassStudent);
        return Result.OK("添加成功！");
    }

    /**
     * 升级
     *
     * @param
     * @return
     */
    @AutoLog(value = "insertUpgradeRelClassStudent-升级")
    @ApiOperation(value = "insertUpgradeRelClassStudent-升级", notes = "insertUpgradeRelClassStudent-升级")
    @GetMapping(value = "/insertUpgradeRelClassStudent")
    public Result<?> insertUpgradeRelClassStudent(@RequestParam(name = "schoolYearId", required = true) String schoolYearId,
                                                  @RequestParam(name = "semesterId", required = true) String semesterId,
                                                  @RequestParam(name = "classId", required = false) String classId,
                                                  @RequestParam(name = "gradeId", required = false) String gradeId) {

        Assert.hasText(classId + gradeId, "班级或者年级Id没有传递，无法升年级");
        Assert.hasText(schoolYearId, "目标学年没有传递，请选择");
        Assert.hasText(semesterId, "目标学期没有传递，请选择");
        RelClassesAndStudentsVO relClassesAndStudentsVO = new RelClassesAndStudentsVO();
        relClassesAndStudentsVO.setSchoolYearId(schoolYearId);
        relClassesAndStudentsVO.setSemesterId(semesterId);
        if (!StringUtil.isEmpty(classId)) {
            relClassesAndStudentsVO.setClassId(classId);
        }
        if (!StringUtil.isEmpty(gradeId)) {
            relClassesAndStudentsVO.setGradeId(gradeId);
        }
        if(relClassStudentService.insertUpgradeRelClassStudent(relClassesAndStudentsVO) > 0){
            return Result.OK("学生班级升级成功！");
        }else {
            return Result.OK("学生班级升级失败！");
        }
    }

    /**
     * 编辑
     *
     * @param relClassStudent
     * @return
     */
    @AutoLog(value = "rel_class_student-编辑")
    @ApiOperation(value = "rel_class_student-编辑", notes = "rel_class_student-编辑")
    @PutMapping(value = "/edit")
    public Result<?> edit(@RequestBody RelClassStudent relClassStudent) {
        relClassStudentService.updateById(relClassStudent);
        return Result.OK("编辑成功!");
    }

    /**
     * 通过id删除
     *
     * @param id
     * @return
     */
    @AutoLog(value = "rel_class_student-通过id删除")
    @ApiOperation(value = "rel_class_student-通过id删除", notes = "rel_class_student-通过id删除")
    @DeleteMapping(value = "/delete")
    public Result<?> delete(@RequestParam(name = "id", required = true) String id) {
        relClassStudentService.removeById(id);
        return Result.OK("删除成功!");
    }

    /**
     * 批量删除
     *
     * @param ids
     * @return
     */
    @AutoLog(value = "rel_class_student-批量删除")
    @ApiOperation(value = "rel_class_student-批量删除", notes = "rel_class_student-批量删除")
    @DeleteMapping(value = "/deleteBatch")
    public Result<?> deleteBatch(@RequestParam(name = "ids", required = true) String ids) {
        this.relClassStudentService.removeByIds(Arrays.asList(ids.split(",")));
        return Result.OK("批量删除成功!");
    }

    /**
     * 通过id查询
     *
     * @param id
     * @return
     */
    @AutoLog(value = "rel_class_student-通过id查询")
    @ApiOperation(value = "rel_class_student-通过id查询", notes = "rel_class_student-通过id查询")
    @GetMapping(value = "/queryById")
    public Result<?> queryById(@RequestParam(name = "id", required = true) String id) {
        RelClassStudent relClassStudent = relClassStudentService.getById(id);
        if (relClassStudent == null) {
            return Result.error("未找到对应数据");
        }
        return Result.OK(relClassStudent);
    }

    /**
     * 导出excel
     *
     * @param request
     * @param relClassStudent
     */
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, RelClassStudent relClassStudent) {
        return super.exportXls(request, relClassStudent, RelClassStudent.class, "rel_class_student");
    }

    /**
     * 通过excel导入数据
     *
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, RelClassStudent.class);
    }

}
